package cn.com.yusys.yusp.admin.service;

import cn.com.yusys.yusp.admin.repository.mapper.AdminSmAuthInfoMapper;
import cn.com.yusys.yusp.commons.exception.MessageConstants;
import cn.com.yusys.yusp.commons.exception.YuspException;
import cn.com.yusys.yusp.commons.mapper.CommonMapper;
import cn.com.yusys.yusp.commons.mapper.QueryModel;
import cn.com.yusys.yusp.commons.service.CommonService;
import cn.com.yusys.yusp.commons.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @version 1.0.0
 * @项目名称: yusp-admin
 * @类名称: AdminSmAuthInfoService
 * @类描述: 认证信息管理服务层
 * @功能描述:
 * @创建人: dusong@yusys.com.cn
 * @创建时间: 2017-12-19 13:42
 * @修改备注:
 * @修改记录: 修改时间    修改人员    修改原因
 * -------------------------------------------------------------
 * @Copyright (c) 2017宇信科技-版权所有
 */
@Service
public class AdminSmAuthInfoService extends CommonService {

    @Autowired
    private AdminSmAuthInfoMapper adminSmAuthInfoMapper;

    @Autowired
    private MessageProviderService messageProviderService;

    @SuppressWarnings("rawtypes")
    @Override
    protected CommonMapper getMapper() {
        return this.adminSmAuthInfoMapper;
    }

    /* (non-Javadoc)
     * @see cn.com.yusys.yusp.commons.service.CommonService#selectByModel(cn.com.yusys.yusp.commons.mapper.QueryModel)
     */
    public <T> List<T> selectByModel(QueryModel model) {
        if (model.getCondition().containsKey("authName") && !StringUtil.isEmpty((String) model.getCondition().get("authName"))) {
            model.getCondition().put("authName", "%" + model.getCondition().get("authName") + "%");
        }
        if (model.getCondition().containsKey("beanName") && !StringUtil.isEmpty((String) model.getCondition().get("beanName"))) {
            model.getCondition().put("beanName", "%" + model.getCondition().get("beanName") + "%");
        }
        if (model.getCondition().containsKey("authRemark") && !StringUtil.isEmpty((String) model.getCondition().get("authRemark"))) {
            model.getCondition().put("authRemark", "%" + model.getCondition().get("authRemark") + "%");
        }
        return this.selectByModel(getMapper(), model);
    }

    /**
     * @方法名称: getAuthKeyValue
     * @方法描述:
     * @参数与返回说明:
     * @算法描述:
     */
    @Transactional(readOnly = true)
    public List<Map<String, String>> getAuthKeyValue() {
        return this.adminSmAuthInfoMapper.getAuthKeyValue(new HashMap<String, String>());
    }

    public int delete(String id) {
        List<String> list = this.adminSmAuthInfoMapper.getSysIdByAuthId(id);
        if (null != list && !list.isEmpty()) {
            throw new YuspException(messageProviderService.getMessage(String.valueOf(MessageConstants.RET_CODE_20100012)));
        }
        return this.adminSmAuthInfoMapper.deleteByPrimaryKey(id);
    }
}
